<template>
  <view class="content-img">
    <eui-nav-bar ref="BcmNavBar" title="商品报损"  backgroundColor="#02B7E6" color="#fff"/>
    <view class="footer" :style="{ height: height + 'px' }" v-if="goodsList.length > 0">
      <view
        style="width: 100%; background: #FFE9DA;padding: 15rpx 20rpx 15rpx 15rpx;color:#FF0000;box-sizing: border-box;font-size: 32rpx;line-height: 35rpx;"
        class="flex just-between align-center">
        <view style="flex: 1;">每次退款仅支持提交1次商品报损，提交成功后会自动生成ERP报损单。</view>
      </view>
      <eui-card-input v-model="breakType" :range="getBcmDictList('breakStatus')" title="报损方式" rightIconType="right"
						type="radio" alignType="left" style="margin-top: 10rpx;" @buttonClick="selectBreakType"/>
      <scroll-view scroll-y="true" class="bg-white"
        style="width: 100%;padding: 10rpx 18rpx;box-sizing: border-box;overflow-y: scroll;" v-if="breakType=='1'">
        <view>
          <view class="goods-item just-between" v-for="(item, index) in goodsList" :key="index">
            <view
              style="width: 40rpx;height: 40rpx;text-align: center;line-height: 40rpx;background-color: #D8D8D8;color: #fff;border-radius: 31rpx;font-size: 32rpx;">
              {{ index + 1 }}
            </view>
            <view style="margin-left: 32rpx;box-sizing: border-box;flex: 1">
              <view>
                <view class="goods-name bold">{{ item.goodsName }}</view>
                <view class="goods-info jus-between">
                  <view style="font-size: 32rpx;color: #626262;">
                    <view>{{ item.goodsCode }}</view>
                    <view>{{ item.goodsBarcode }}</view>
                  </view>
                  <view class="flex" style="flex: 1;justify-content: space-between;margin-left: 100rpx;">
                    <view class="goods-quantity bold"
                      :style="{ 'color': item.refundNum > 1 ? '#E43E00' : '#626262' }">x<text>
                        {{ item.refundNum }}</text>
                    </view>
                    <view class="flex align-center" style="font-size: 32rpx;">
                      <eui-uicons type="minus-filled" size="60"
                        :style="{ 'color': item.applyRefundNum == 0 ? '#D8D8D8 !important' : '#85B6FD !important' }"
                        @click="minusNum(index)"></eui-uicons>
                      <view style="color: #626262;width: 100rpx;" class="text-center">{{ item.applyRefundNum }}</view>
                      <eui-uicons type="plus-filled" size="60"
                        :style="{ 'color': item.applyRefundNum == item.refundNum ? '#D8D8D8 !important' : '#85B6FD !important' }"
                        @click="plusNum(index)"></eui-uicons>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </view>
          <view style="padding: 22rpx;font-size: 32rpx;">
            <view class="flex just-between bold">
              <view>报损</view>
              <view>{{ sumNum() }}件</view>
            </view>
          </view>
        </view>
      </scroll-view>
      <view style="padding: 22rpx;font-size: 32rpx;" v-if="breakType == '0'">
        <view class="flex just-between bold">
          <view>报损</view>
          <view>{{ sumTotalNum() }}件</view>
        </view>
      </view>
    </view>
    <view class="bottom" v-if="goodsList.length > 0">
      <eui-button title="确定" type="max" singleTag="confirm" @buttonClick="buttonClick"
        style="width: 100%;height: 86rpx;line-height: 86rpx;border-radius: 0;margin: 0;padding:0"
        :disabled="sumNum() == 0 && breakType == '1'"></eui-button>
    </view>
  </view>
</template>
<script setup lang="js">
import { mapState, mapMutations } from 'vuex';
import { ref, computed, onMounted } from 'vue';
import { onLoad } from "@dcloudio/uni-app";
import service from '../../../common/service';
import { bcmRequest, getBcmUrl } from '../../../hooks/bcmServices';
import { getBcmDictList, getBcmDictName, getBcmDictItem, formatPrice } from '../../../hooks/bcmUseUntils';
import _img_blank_order from '../../../static/img/status/blank_order.png';

const bcmUrl = getBcmUrl()

const goodsList = ref([])
const breakType = ref('0');
const jumpFromType = ref('1');
const afsOrderId = ref('')
const height = ref(0);

const plusNum = function (index) {
  if (goodsList.value[index].applyRefundNum < goodsList.value[index].refundNum) {
    goodsList.value[index].applyRefundNum++;
  }
}

const minusNum = function (index) {
  if (goodsList.value[index].applyRefundNum > 0) {
    goodsList.value[index].applyRefundNum--;
  }
}

const sumNum = function () {
  return goodsList.value.filter(a => a.applyRefundNum != 0).reduce((accumulator, currentValue) => {
    let num = Number(currentValue.applyRefundNum);
    if (!isNaN(num)) {
      return accumulator + num;
    } else {
      return accumulator;
    }
  }, 0);
}

const sumTotalNum = function () {
  return goodsList.value.filter(a => a.refundNum != 0).reduce((accumulator, currentValue) => {
    let num = Number(currentValue.refundNum);
    if (!isNaN(num)) {
      return accumulator + num;
    } else {
      return accumulator;
    }
  }, 0);
}


const selectBreakType = (e) => {
  breakType.value = e;
}

const buttonClick = async function (t) {
  const { singleTag } = t;
  let list = goodsList.value.filter(a => a.applyRefundNum != 0);
  switch (singleTag) {
    case 'confirm':
      let params = {
          itemList: [{
            afsOrderId: afsOrderId.value,
            breakageGoodsList: list.map(v => {
                return {
                    afsOrderGoodsId: v.afsOrderGoodsId,
                    breakageNum: v.applyRefundNum
                }
            })
          }],
          status: breakType.value
      }
      if(['0','2'].includes(params.status)){
            delete params.itemList[0].breakageGoodsList
        }else{
            if (params.itemList[0].breakageGoodsList?.filter(a => !a.breakageNum)?.length == params.itemList[0].breakageGoodsList?.length){
                ENotification.info('请完善退损数量～')
                return false
            }
        }
      service.enjoyShowLoading('正在处理...');
      await bcmRequest(bcmUrl.orderApi, '售后单表', 'SetBreakageStatus', params);
      service.enjoyHideLoading();
      uni.showToast({
        title: '商品退损操作成功',
        icon: 'none'
      });
      setTimeout(() => {
        uni.navigateBack({
          delta: jumpFromType.value=='1'?2:1
        });
        uni.$emit('REFRESH_ORDER_DETAIL_DATA');
        uni.$emit('REFRESH_ORDER_SEARCH_DATA');
      }, 1000)
      break;
  }
}

const billQuery = async (id = BillId.value) => {
    service.enjoyShowLoading('正在加载...');
    const req = {
        BillId: id,
    };
    const result = await bcmRequest(bcmUrl.orderApi, '售后单表', 'EnjoyBillQuery', req);
    service.enjoyHideLoading();
    if (!!result.ds) {
        if (result.ds1.length > 0) {
            result.ds1?.map(v => {
              v.applyRefundNum = 0
            })
            goodsList.value = result.ds1
        }
    } else {
        uni.showToast({
            duration: 3500,
            icon: 'none',
            title: '未查询到数据',
            mask: true,
        });
    }
}


onLoad((options) => {
    afsOrderId.value = options.afsOrderId;
    jumpFromType.value = options.type;
    billQuery(afsOrderId.value)
})

</script>
<style lang="scss" scoped>
@import "@/static/font/enjoyicon/iconfont.css";
@import "../common/common.css";

.content-img {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  // background: $uni-background-img;
  background-size: 100% 150px;
}

uni-checkbox {
  ::v-deep .uni-checkbox-input {
    // width: 22rpx;
    // height: 22rpx;
    transform: scale(.6);
    // margin-right: 17rpx;
  }
}

.footer {
  display: flex;
  flex-direction: column;
  flex: 1;
  background: #f5f7fa;
}

.align-center {
  align-items: center;
}

.bottom {
  height: 86rpx;
}

.bottom-1 {
  display: flex;
  justify-content: center;
  flex: 0 0 auto;
  height: 70rpx;
  padding: $uni-spacing-col-sm * 2 $uni-spacing-row-sm;
  background: $uni-bg-color;
}

//list
.goods-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10rpx;
  border-bottom: 1rpx solid #e1e1e1;
}

.goods-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.goods-name {
  margin-bottom: 2px;
  font-size: 32rpx;
  line-height: 35rpx;
}

.goods-price {
  font-size: 32rpx;
  text-align: right;
}

.goods-quantity {
  color: #626262;
  font-size: 32rpx;
}

.popup-content {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 0;
  background-color: #fff;

  .popup-title {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    height: 100rpx;
    padding-right: 36rpx;
    padding-left: 30rpx;
    font-size: 33rpx;
    line-height: 100rpx;
    border-bottom: 1px solid #f1f1f1;
  }

  textarea {
    box-sizing: border-box;
    width: 100%;
    height: 80px;
    padding: 5px;
    margin: 5px 0;
    border: 1px solid #ccc;
    border-radius: 5px;
  }
}
</style>
